-- Kind of a Config. Everything else configurable ingame.

-- Quest Watch Frame Config
local wfwidth = 150 -- Width of Watchframe
local wfheight = 500 -- Height of Watchframe
local wfscale = 0.8 -- Watch Frame Scale

-- Clock Font, Fontsize, Fontstyle
local font = [[Interface\AddOns\MidgetMap\media\marke.ttf]]
local fontsize = 8
local fontstyle = "MONOCHROMEOUTLINE"
local back = {
	bgFile = [[Interface\ChatFrame\ChatFrameBackground]], insets = {top = -1, bottom = -1, left = -1, right = -1}
}
 
local mailicon = [[Interface\Addons\MidgetMap\media\mail]] -- mail texture

-- end config

--[[All CREDIT DUE TO 
--==Ichik, Alza, Blooblahguy, Tukz, Dawn, Xrystal, Katae, Freebaser, Nightcracker, & Dainton==--
for help and reference and Many HUGE HUGE thanks to the WoWInterface 
Forums and Community for reference as well as WoWWiki and WoW Programming.  I looked at MANY 
MANY MANY minimaps, and tried to add as much features as possible for the meantime.

----------------------------------------------------------------------------------------------------------------------------------------------------
====================================================================================================================================================
____________________________
CHANGE LOG v 1.6 10/21/2010

Updated for Cataclysm 4.0.1. Sorry for the wait. Got a hint to show the LFD tooltip no matter where you put the minimap from BgMinimap. Thanks!

_______________
1.5.2 9/4/2010

Added right Click to toggle Calendar. Added notification on Pending Invites.
_________________
v 1.5.1 9/3/2010

Made sure it works with Cataclysm. Everything seems to work fine, let me know if anything comes up. Added Time to the buttom of the minimap.

Config on top of Lua added for Font, Fontsize, and fontstyle. I may or may not add a config for this later.

Made small adjustments. Added tiny config on top for Quest Watch Frame if you want to change it up. Also added small config for font, fontsize, and fontstyle.

_________________
v 1.4.2 7/22/2010

Made it to where the watch frame saves position now upon logging in and out per char, adding it to saved variables. Also added ingame slash
commands to change the strata. Will save upon logging in and out per char, adding it to saved variables as well. I'm getting better at this!
_________________
V 1.4.1 7/21/2010

Shape changing works finally! Thanks Dainton!
_______________
v 1.4 7/17/2010

Made a few changes in code. Got rid of background, use litepanels or kgpanels for background :P I made it to where you can change minimap to round or square with ingame slash commands. /shape round or /shape square.

Also changed the mail icon, and changed some of the icons set points.
_______________
v 1.3 7/13/2010

jump on the version! I've been working on it without release.I cleaned more code up, got rid of more than onevent handlers. removed the menu button, because it was being stupid. Scale saves now! This is now moved out of beta and into release. Please posts bugs if you find them. Thanks!
_______________
v 0.5 5/26/2010

Cleaned the code a bit more, made a menu button on hover only below the minimap for ingame menu for your character pane, lfd, lfr, pvp, gm help, etc. THIS TIME FOR SURE I fixed the instance difficulty. IT WILL SHOW NOW! Sorry

Known issues fixed - Heroic Flag not showing. Is now showing. Ping while right clicking minimap now not doing that because I added the hover game menu button.
_______________
v 0.4 5/26/2010

Cleaned up the code a bit. Made all ingame text green. Added slash command /midgethelp to display all slash commands for the minimap. Added right click function to show drop down menu for ingame menu i.e. Character, LFD, LFR, PvP, GM Help, etc. Thanks to Alza! Fixed the Heroic Flag Instance Difficulty, it should show now.
_______________
v 0.3 5/25/2010

Tiny change in code thanks to Seerah! Made a paragraph of code down to a sentence basically. Now able to type ingame /scale or /mmscale to any scale to your liking.
_______________
v 0.2 5/25/2010

Added Scale slash commands ingame. type /scale or /mmscale ingame to configure scaling. (Note it only goes from 0.6 to 1.7 scale).
_______________
v 0.1 5/25/2010

Initial release

]]--

local ver = 1.6

--==Defaults it loads with when you first load the AddOn==--

local defaults = { 
align = "TOPRIGHT", 
x = -25, 
y = -25, 
scale = 1, 
locked = true,
shape = "SQUARE",
wfx = 20,
wfy = -200,
wfalign = "TOPLEFT",
mstrata = "MEDIUM"
}

local shapes = {"Square", "Circle"}

--==Saved Variables - Taken from ItsyMm==-- 
--==CREDIT ALL DUE TO Katae==--

local MidgetMap, shit, db = CreateFrame'frame', "|cFF00FFFFMidgetMap|r"
MidgetMap:RegisterEvent'ADDON_LOADED'
MidgetMap:SetScript("OnEvent", function(self)
	if self then
		if not MidgetMapDB then print(shit,": No Midgets were Harmed in the Making of this AddOn. Type /mm for help") end
	end	
	if not MidgetMapDB or not MidgetMapDB.ver or MidgetMapDB.ver < 1.6 then
		MidgetMapDB = defaults MidgetMapDB.ver = ver
	
		
	end db = MidgetMapDB
	Minimap:ClearAllPoints()
	Minimap:SetPoint(db.align, UIParent, db.align, db.x, db.y)
	Minimap:SetScale(db.scale)
	Minimap:SetMovable(true)
	Minimap:SetUserPlaced(true)
	Minimap:SetClampedToScreen(true)
	
	if db.shape == "SQUARE" then
		mask = "Interface\\Buttons\\WHITE8X8"
	elseif db.shape == "ROUND" then
		mask = "textures\\MinimapMask"
	end
	Minimap:SetMaskTexture(mask)
end)

LFDSearchStatus:SetClampedToScreen(true)

--==SLASH COMMANDS==--
-- reload the ui
SlashCmdList["RELOADUI"] = function() ReloadUI() end
SLASH_RELOADUI1 = "/rl"
-- Calendar
SlashCmdList["CALENDAR"] = function()
	ToggleCalendar()
end
SLASH_CALENDAR1 = "/cal"
SLASH_CALENDAR2 = "/calendar"


Minimap:EnableMouseWheel(true)
Minimap:SetScript('OnMouseWheel', function(self, delta)
    if delta > 0 then
        Minimap_ZoomIn()
    else
        Minimap_ZoomOut() 
    end
end)

--==Scaling of Minimap==--

local function SCALEMM(msg)
	if msg == "reset" then
		MidgetMapDB = nil
		MidgetMap:GetScript("OnEvent")()
		print(shit, "The settings were reset.")
	elseif msg:match'scale [%d%.]+' then
		local scale = msg:match'scale ([%d%.]+)'
		Minimap:SetScale(scale)
		db.scale = scale
	
	elseif msg == "lock" then
		db.locked = not db.locked
		print(shit, (db.locked and "Locked." or "Unlocked. Hold Shift and Mouse down to move."))
	
	else
		print(shit..": List of commands are as follows:")
		print("|cFF00FFFF /mm lock|r Unlock or lock the minimap. Hold Shift and drag.")
		print("|cFF00FFFF /mm scale #|r Change the scale. Example: |cFFFFFF00/mm scale 0.8|r Current scale is:",format("%.2f",db.scale))
		print("|cFF00FFFF /mm reset|r Reset MidgetMap to default.")
		print("|cFF00FFFF /rl|r Reload your UI.")
		print("|cFF00FFFF /cal|r Access the Calendar.")
		print("|cFF00FFFF /shape shape here|r Change the shape. Example: |cFFFFFF00/shape round|r or |cFFFFFF00/shape square|r")
		print("|cFF00FFFF /mmstrata strata here|r to change the strata. Example: |cFFFFFF00/mmstrata high|r Available stratas are background, low, medium, high, and dialog.")
		print("|cFF00FFFF /wf|r to lock or unlock the watchframe. Drag by the white text.")
	end
end

SLASH_SCALEMM1 = "/mm";
SlashCmdList["SCALEMM"] = SCALEMM

local function MMSTRATA(msg)
	local msg = strlower(msg)
	
	if msg == "high" then
		Minimap:SetFrameStrata("HIGH")
		db.mstrata = "HIGH"
		print(shit..": Set Minimap Strata to High.")
	elseif msg == "medium" then
		Minimap:SetFrameStrata("MEDIUM")
		db.mstrata = "MEDIUM"
		print(shit..": Set Minimap Strata to Medium.")
	elseif msg == "low" then
		Minimap:SetFrameStrata("MEDIUM")
		db.mstrata = "LOW"
		print(shit..": Set Minimap Strata to Low.")
	elseif msg == "background" then
		Minimap:SetFrameStrata("BACKGROUND")
		db.mstrata = "BACKGROUND"
		print(shit..": Set Minimap Strata to Background.")
	elseif msg == "dialog" then
		Minimap:SetFrameStrata("DIALOG")
		db.mstrata = "DIALOG"
		print(shit..": Set Minimap Strata to Dialog.")
	else
		print(shit..": Not a valid command.")
		print(shit..": List of commands are as follows:")
		print("|cFF00FFFF /mm lock|r Unlock or lock the minimap. Hold Shift and drag.")
		print("|cFF00FFFF /mm scale #|r Change the scale. Example: |cFFFFFF00/mm scale 0.8|r Current scale is:",format("%.2f",db.scale))
		print("|cFF00FFFF /mm reset|r Reset MidgetMap to default.")
		print("|cFF00FFFF /rl|r Reload your UI.")
		print("|cFF00FFFF /cal|r Access the Calendar.")
		print("|cFF00FFFF /shape shape here|r Change the shape. Example: |cFFFFFF00/shape round|r or |cFFFFFF00/shape square|r")
		print("|cFF00FFFF /mmstrata strata here|r to change the strata. Example: |cFFFFFF00/mmstrata high|r Available stratas are background, low, medium, high, and dialog.")
		print("|cFF00FFFF /wf|r to lock or unlock the watchframe. Drag by the white text.")
	end
end

SLASH_MMSTRATA1 = "/mmstrata"
SlashCmdList["MMSTRATA"] = MMSTRATA



local function SHAPE(msg)
	local msg = strlower(msg)
	
	if msg == "square" then
		Minimap:SetMaskTexture("Interface\\Buttons\\WHITE8X8")
		db.shape = "SQUARE"
		function GetMinimapShape() return "SQUARE" end
		print(shit.."|cFF00FFFF: Set to Square.|r")
	elseif msg == "round" then
		Minimap:SetMaskTexture("textures\\MinimapMask")
		db.shape = "ROUND"
		function GetMinimapShape() return "ROUND" end
		print(shit.."|cFF00FFFF: Set to Round.|cFF00FFFF")
	else
		print(shit..": Not a valid command.")
		print(shit..": List of commands are as follows:")
		print("|cFF00FFFF /mm lock|r Unlock or lock the minimap. Hold Shift and drag.")
		print("|cFF00FFFF /mm scale #|r Change the scale. Example: |cFFFFFF00/mm scale 0.8|r Current scale is:",format("%.2f",db.scale))
		print("|cFF00FFFF /mm reset|r Reset MidgetMap to default.")
		print("|cFF00FFFF /rl|r Reload your UI.")
		print("|cFF00FFFF /cal|r Access the Calendar.")
		print("|cFF00FFFF /shape shape here|r Change the shape. Example: |cFFFFFF00/shape round|r or |cFFFFFF00/shape square|r")
		print("|cFF00FFFF /mmstrata strata here|r to change the strata. Example: |cFFFFFF00/mmstrata high|r Available stratas are background, low, medium, high, and dialog.")
		print("|cFF00FFFF /wf|r to lock or unlock the watchframe. Drag by the white text.")
	end
end

SLASH_SHAPE1 = "/shape"
SlashCmdList["SHAPE"] = SHAPE

--==move the watchframe NAO==--

local function OnMouseUp(self)
	if self.IsMoving then
		self.IsMoving = false
		Minimap:StopMovingOrSizing()
		db.align,_,_,db.x,db.y = Minimap:GetPoint()
		Minimap:SetScript("OnMouseUp", Minimap_OnClick)
	end
end
Minimap:SetScript("OnMouseDown", function(self, button)
	if button == "LeftButton" and IsShiftKeyDown() and not db.locked then
		self.IsMoving = true
		Minimap:StartMoving()
		Minimap:SetScript("OnMouseUp", OnMouseUp)
	end
end)

--==QUESTTRACKER==--
local wf = WatchFrame
local wfmove = false 

wf:SetMovable(true);
wf:SetClampedToScreen(false); 
wf:ClearAllPoints()
wf:SetPoint(defaults.wfalign, UIParent, defaults.wfalign, defaults.wfx, defaults.wfy)
wf:SetWidth(wfwidth)
wf:SetHeight(wfheight)
wf:SetUserPlaced(true)
wf.SetPoint = function() end
wf:SetScale(wfscale)


local function WATCHFRAMELOCK()
	if wfmove == false then
		wfmove = true
		print("|cFF00FFFFQuest Watch Frame unlocked for drag|r")
		wf:EnableMouse(true);
		wf:RegisterForDrag("LeftButton"); 
		wf:SetScript("OnDragStart", wf.StartMoving); 
		wf:SetScript("OnDragStop", wf.StopMovingOrSizing);
		db.wfalign,_,_,db.wfx,db.wfy = wf:GetPoint()
	elseif wfmove == true then
		wf:EnableMouse(false);
		wfmove = false
		print("|cFF00FFFFQuest Watch Frame locked|r")
	else
		print(shit..": Not a valid command.")
		print(shit..": List of commands are as follows:")
		print("|cFF00FFFF /mm lock|r Unlock or lock the minimap. Hold Shift and drag.")
		print("|cFF00FFFF /mm scale #|r Change the scale. Example: |cFFFFFF00/mm scale 0.8|r Current scale is:",format("%.2f",db.scale))
		print("|cFF00FFFF /mm reset|r Reset MidgetMap to default.")
		print("|cFF00FFFF /rl|r Reload your UI.")
		print("|cFF00FFFF /cal|r Access the Calendar.")
		print("|cFF00FFFF /shape shape here|r Change the shape. Example: |cFFFFFF00/shape round|r or |cFFFFFF00/shape square|r")
		print("|cFF00FFFF /mmstrata strata here|r to change the strata. Example: |cFFFFFF00/mmstrata high|r Available stratas are background, low, medium, high, and dialog.")
		print("|cFF00FFFF /wf|r to lock or unlock the watchframe. Drag by the white text.")
	end
end

SLASH_WATCHFRAMELOCK1 = "/wf"
SlashCmdList["WATCHFRAMELOCK"] = WATCHFRAMELOCK

--==TRACKING ICON TOP LEFT==--
MiniMapTracking:ClearAllPoints()
MiniMapTracking:SetParent(Minimap)
MiniMapTracking:SetPoint('TOPLEFT', -2, 2)
MiniMapTracking:SetAlpha(.8)
MiniMapTrackingBackground:Hide()
MiniMapTrackingButtonBorder:SetTexture(nil)
MiniMapTrackingButton:SetHighlightTexture(nil)
MiniMapTrackingIconOverlay:SetTexture(nil)
MiniMapTrackingIcon:SetTexCoord(0.065, 0.935, 0.065, 0.935)

--==MAIL FRAME BOTTOM==--
MiniMapMailFrame:ClearAllPoints()
MiniMapMailFrame:SetParent(Minimap)
MiniMapMailFrame:SetPoint('BOTTOM', 0, 30)
MiniMapMailFrame:SetHeight(10)
MiniMapMailBorder:Hide()
MiniMapMailIcon:SetTexture(mailicon)

--==BATTLEGROUND ICON==--
MiniMapBattlefieldFrame:SetFrameLevel(3)
MiniMapBattlefieldBorder:Hide()

--==LFG ICON==--
MiniMapLFGFrameBorder:Hide()

--==BATTLEGROUND ICON TOP==--
MiniMapBattlefieldFrame:ClearAllPoints()
MiniMapBattlefieldFrame:SetPoint("TOP", Minimap, 0, -3)
MiniMapBattlefieldFrame:SetScale(.9)
MiniMapBattlefieldBorder:Hide()

--==Minimap Instance Difficulty TOP RIGHT==--
MiniMapInstanceDifficulty:ClearAllPoints()
MiniMapInstanceDifficulty:SetPoint("TOPRIGHT", Minimap, 0, 0)
MiniMapInstanceDifficulty:SetFrameStrata("HIGH")

--==LFG FRAME BOTTOM RIGHT==--
MiniMapLFGFrame:ClearAllPoints()
MiniMapLFGFrame:SetParent(Minimap)
MiniMapLFGFrame:SetPoint("BOTTOMRIGHT", Minimap, 0, -3)
MiniMapLFGFrame:SetScale(.9)

-- Hiding Shit

MiniMapVoiceChatFrame:Hide()
MiniMapWorldMapButton:Hide()
MinimapZoneTextButton:Hide()
MiniMapMailBorder:Hide()
MinimapBorderTop:Hide()
MinimapNorthTag:SetAlpha(0)
MinimapZoomOut:Hide()
MinimapZoomIn:Hide()
MinimapBorder:Hide()
GameTimeFrame:Hide()
MiniMapBattlefieldBorder:Hide()
MiniMapLFGFrameBorder:Hide()

-- time
LoadAddOn('Blizzard_TimeManager')

local clockFrame, clockTime = TimeManagerClockButton:GetRegions()
clockFrame:Hide()
clockTime:SetFont(font, fontsize, fontstyle)
    if(CalendarGetNumPendingInvites() > 0) then
		clockTime:SetTextColor(0, 1, 0)
	else
		clockTime:SetTextColor(1, 1, 1)
    end
TimeManagerClockButton:SetPoint("CENTER", Minimap, "BOTTOM", -2, 10)

TimeManagerClockButton:EnableMouse(false)

-- Right click Minimap for Calendar.
local bla = Minimap:GetScript("OnMouseUp")
	Minimap:SetScript("OnMouseUp", function(self, click)
		if click == "RightButton" then
			if not CalendarFrame then
				LoadAddOn("Blizzard_Calendar")
			end
			if CalendarFrame:IsShown() then
				CalendarFrame:Hide()
			else
				CalendarFrame:Show()
			end
		
		return
		
		end
		
	bla(self)
end
			
)

--==POSITION & SCALING==--
Minimap:SetFrameStrata(defaults.mstrata)
Minimap:ClearAllPoints()
Minimap:SetPoint(defaults.x, defaults.y)
MinimapCluster:ClearAllPoints()
MinimapCluster:SetAllPoints(Minimap)
MinimapCluster:SetUserPlaced(true)

function GetMinimapShape() return db.shape end